//
//  MineViewController.m
//  Code
//
//  Created by 韩东 on 17/10/24.
//  Copyright © 2017年 HD. All rights reserved.
//

#import "MineViewController.h"
#import "YSLDraggableCardContainer.h"
#import "CardView.h"
#import "XGViewController.h"
#import "GHViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"

#define APPDELEGATE   ((AppDelegate*)[[UIApplication sharedApplication] delegate])
@interface MineViewController () <YSLDraggableCardContainerDelegate, YSLDraggableCardContainerDataSource,UIAlertViewDelegate>

@property (nonatomic, strong) YSLDraggableCardContainer *container;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation MineViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *image = [[UIImage alloc]init];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    self.navigationController.navigationBar.shadowImage = image;
    self.title = @"个人中心";
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.colors = @[(__bridge id)[UIColor cz_colorWithHex:0x3082ed].CGColor,(__bridge id)[UIColor cz_colorWithHex:0x56cbf2].CGColor];
    gradientLayer.startPoint = CGPointMake(1, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    [self.view.layer addSublayer:gradientLayer];
    
    _container = [[YSLDraggableCardContainer alloc]init];
    _container.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _container.backgroundColor = [UIColor clearColor];
    _container.dataSource = self;
    _container.delegate = self;
    _container.canDraggableDirection = YSLDraggableDirectionLeft | YSLDraggableDirectionRight | YSLDraggableDirectionUp;
    [self.view addSubview:_container];
    
    
    [self loadData];
    
    [_container reloadCardContainer];
}

- (void)loadData
{
    _datas = [NSMutableArray array];
    
    for (int i = 0; i < 3; i++) {
        
        
        NSDictionary *dict = @{@"image" : [NSString stringWithFormat:@"photo_sample_0%d",i + 1],
                               @"name" : @"Sing Out",
                               @"itemL" :@"退出"
                               };
        
        [_datas addObject:dict];
    }
    
    
}



#pragma mark -- YSLDraggableCardContainer DataSource
- (UIView *)cardContainerViewNextViewWithIndex:(NSInteger)index
{
    NSDictionary *dict = _datas[index];
    CardView *view = [[CardView alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height/6, self.view.frame.size.width - 20, self.view.frame.size.height/1.5 )];
//    view.backgroundColor = [UIColor whiteColor];
    
    
    view.imageView.image = [UIImage imageNamed:dict[@"image"]];
    
    
    if ([dict[@"image"]  isEqual: @"photo_sample_01"]) {
        view.label.text = @"Change cell phone";
        view.itemLable.text = @"更换手机";
    }
    if ([dict[@"image"]  isEqual: @"photo_sample_02"]) {
        view.label.text = @"Modify password";
        view.itemLable.text = @"修改密码";
    }
    if ([dict[@"image"]  isEqual: @"photo_sample_03"]) {
        view.label.text = @"Sing Out";
        view.itemLable.text = @"退出";
    }
    
    view.backImage.image = [UIImage imageNamed:@"phone_bg"];
    view.rightImage.image = [UIImage imageNamed:@"right_arrow"];
    view.leftImage.image = [UIImage imageNamed:@"left_arrow"];
    return view;
}

- (NSInteger)cardContainerViewNumberOfViewInIndex:(NSInteger)index
{
    return _datas.count;
}

#pragma mark -- YSLDraggableCardContainer Delegate
- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didEndDraggingAtIndex:(NSInteger)index draggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection
{
    if (draggableDirection == YSLDraggableDirectionLeft) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
}



- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didSelectAtIndex:(NSInteger)index draggableView:(UIView *)draggableView
{
    NSLog(@"++ index : %ld",(long)index);
    if (index ==0) {
         [self.navigationController pushViewController:[[GHViewController alloc]init] animated:YES];
        
    }
    if (index == 1) {
        [self.navigationController pushViewController:[[XGViewController alloc]init] animated:YES];
    }
    if (index == 2) {
        
        //到登录页
         [AppDelegate shareDelegate].window.rootViewController=[[LoginViewController alloc]init];
        
        
    }
}

- (void)cardContainerViewDidCompleteAll:(YSLDraggableCardContainer *)container;
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [container reloadCardContainer];
    });
}



@end
